<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('IN_COMPONENT_CONTROLLER') or die();

class OrdersSummaryComponent
{
	function OrdersSummaryComponent()
	{
	}

	function action(&$params, $action)
	{
		return $this->query($params);
	}

	function query(&$params) 
	{
		$language_id = (int)$params['languageId'];
		$accountId = (int)$params['accountId'];
		$offset = (int)$params['os_offset'];
		$orderby = (string)$params['os_orderby'];
		if ($orderby == null) $orderby = 'order by o.orders_id desc';
		else $orderby = 'order by ' . mysql_real_escape_string($orderby);
		$max_rows = MAX_DISPLAY_ORDER_HISTORY;

		if ($accountId > 0)
		{
			$sqlQuery = "select o.orders_id, o.delivery_name, DATE_FORMAT(o.date_purchased, '%Y-%m-%d %H:%i:%s') as date_purchased, " . 
			'os.orders_status_name, ot.value, count(op.orders_products_id) as product_number, o.currency, o.currency_value ' .
			' from ' . TABLE_ORDERS . ' o, ' . TABLE_ORDERS_STATUS . ' os, ' . TABLE_ORDERS_TOTAL . ' ot, ' . TABLE_ORDERS_PRODUCTS . ' op ' .
			'where customers_id = \''. $accountId .'\' ' .
			'and o.orders_status = os.orders_status_id ' .
			'and os.language_id = \'' . $language_id . '\' ' .
			'and o.orders_id = ot.orders_id and ot.class = \'ot_total\' ' .
			'and o.orders_id = op.orders_id and o.orders_status > -1 ' .
			'group by o.orders_id ' .
			$orderby . ' limit ' . $offset . ', ' . $max_rows;

			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			$i = 0;
			while($row = mysql_fetch_array($dataReturned))
			{
				$value{'OrdersSummaryComponent'}{$i}{'id'}= (int)$row['orders_id'];
				$value{'OrdersSummaryComponent'}{$i}{'ship_to_name'}= $row['delivery_name'];
				$value{'OrdersSummaryComponent'}{$i}{'date_purchased'}= $row['date_purchased'];
				$value{'OrdersSummaryComponent'}{$i}{'status'}= $row['orders_status_name'];
				$value{'OrdersSummaryComponent'}{$i}{'value'}= (double)$row['value'];
				$value{'OrdersSummaryComponent'}{$i}{'product_number'}= (int)$row['product_number'];
				$value{'OrdersSummaryComponent'}{$i}{'currency_code'}= $row['currency'];
				$value{'OrdersSummaryComponent'}{$i}{'currency_value'}= (double)$row['currency_value'];
				$i++;
			}

			$sqlQuery = 'select orders_id from ' . TABLE_ORDERS . ' where customers_id = \''. $accountId .'\' ';
			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			$cnt = 0;
			while($row = mysql_fetch_array($dataReturned))
			{
				$cnt++;
			}
			$value{'OrdersSummaryComponentMetaData'}{'max_rows'}= (int)$max_rows;
			$value{'OrdersSummaryComponentMetaData'}{'page_links_max'} = (int)MAX_DISPLAY_PAGE_LINKS;
			$value{'OrdersSummaryComponentMetaData'}{'prev_next_bar_location'} = (int)PREV_NEXT_BAR_LOCATION;
			$value{'OrdersSummaryComponentMetaData'}{'offset'}= $offset;
			$value{'OrdersSummaryComponentMetaData'}{'rows'}= $i;
			$value{'OrdersSummaryComponentMetaData'}{'total_rows'}= $cnt;
		}
		return $value;
	}
}
?>
